from .models import *
from rest_framework import serializers
import re
from django.forms import ValidationError
from staff.models import StaffModel


# 操作日志序列化器
class OperateLogSer(serializers.ModelSerializer):
    class Meta:
        model = Operate_LogModel
        fields = "__all__"


# 权限角色序列化器
class RoleSer(serializers.ModelSerializer):
    class Meta:
        model = RoleModel
        fields = ["id", "name", "desc"]

    def validate(self, attrs):
        name = attrs.get("name")
        desc = attrs.get("desc")
        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$", name):
            raise ValidationError(f'{name} 角色名称由2到30位汉字、数字、字母或下划线组成！')
        if desc != None:
            if not re.findall(r"^[\u4e00-\u9fa5\w\W]{2,256}$", desc):
                raise ValidationError(f'{name} 简介由2到256位汉字、数字、字母或下划线组成！')
        return attrs


# 菜单序列化器
class MenuSer(serializers.ModelSerializer):
    class Meta:
        model = MenuModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")
        icon = attrs.get("icon")
        order = attrs.get("order")
        menu_id = attrs.get("menu_id")
        # role_id = attrs.get("role_id")
        print(menu_id)
        # print(role_id)
        print(order)
        print(icon)
        print(name)

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$", name):
            raise ValidationError(f'{name} 名称由2到30位汉字、数字、字母或下划线组成！')
        # if icon != None:
        #         #     if not re.findall(r"^[\w\W]{2,256}$", icon):
        #         #         raise ValidationError(f'{icon} 图标为2到256位数字、字母或下划线组成！')

        if not re.findall(r"^\d$", f"{order}"):
            raise ValidationError(f'{order} order由数字组成！')

        if menu_id != None:
            menu_list = MenuModel.objects.filter(is_delete=False).values("id")
            menu_list = [i.get("id") for i in menu_list]
            if menu_id not in menu_list:
                raise ValidationError(f"{menu_id} 该菜单不存在！")

        return attrs


# 权限序列化器
class PermissionSer(serializers.ModelSerializer):
    class Meta:
        model = PermissionModel
        fields = "__all__"


# 部门序列化器
class DepartmentSer(serializers.ModelSerializer):
    class Meta:
        model = DepartmentModel
        fields = [
            "id",
            "name",
            "desc",
            "staff_id",
            "department_id"
        ]
    def validate(self, attrs):
        name = attrs.get("name")
        desc = attrs.get("desc")
        staff_id = attrs.get("staff_id")
        department_id = attrs.get("department_id")
        print('name', name)

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$",name):
            raise ValidationError(f'{name} 部门名称由2到30位汉字、数字、字母或下划线组成！')

        if desc != None:
            if not re.findall(r"^[\u4e00-\u9fa5\w\W]{2,256}$",desc):
                raise ValidationError(f'{name} 简介由2到256位汉字、数字、字母或下划线组成！')

        if staff_id != None:
            staff_list = StaffModel.objects.filter(is_delete=False).values("id")
            staff_list = [i.get("id") for i in staff_list]
            if staff_id not in staff_list:
                raise ValidationError(f"{staff_id} 该员工不存在！")
        if department_id != None:
            department_list = DepartmentModel.objects.filter(is_delete=False).values("id")
            department_list = [i.get("id") for i in department_list]
            if department_id not in department_list:
                raise ValidationError(f"{department_id} 该部门不存在！")

        return attrs


# 职级序列化器
class RankSer(serializers.ModelSerializer):
    class Meta:
        model = RankModel
        fields = ["id","name","desc"]
    def validate(self, attrs):
        name = attrs.get("name")
        desc = attrs.get("desc")
        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$",name):
            raise ValidationError(f'{name} 职级名称由2到30位汉字、数字、字母或下划线组成！')
        if desc != None:
            if not re.findall(r"^[\u4e00-\u9fa5\w\W]{2,256}$",desc):
                raise ValidationError(f'{name} 简介由2到256位汉字、数字、字母或下划线组成！')
        return attrs


# 职位角色中间表序列化器
# class PositionRoleSer(serializers.ModelSerializer):
#     class Meta:
#         model = Position_RoleModel
#         fields = "__all__"


# 职位序列化器
class PositionSer(serializers.ModelSerializer):
    class Meta:
        model = PositionModel
        fields = [
            "id",
            "name",
            "rank_id",
            "department_id",
            "desc",
        ]
    def validate(self, attrs):
        name = attrs.get("name")
        rank_id = attrs.get("rank_id")
        department_id = attrs.get("department_id")
        desc = attrs.get("desc")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$",name):
            raise ValidationError(f'{name} 职位名称由2到30位汉字、数字、字母或下划线组成！')

        if desc != None:
            if not re.findall(r"^[\u4e00-\u9fa5\w\W]{2,256}$",desc):
                raise ValidationError(f'{name} 简介由2到256位汉字、数字、字母或下划线组成！')

        rank_list = RankModel.objects.filter(is_delete=False).values("id")
        rank_list = [i.get("id") for i in rank_list]
        if rank_id not in rank_list:
            raise ValidationError(f"{rank_id} 该职级不存在！")

        staff_list = DepartmentModel.objects.filter(is_delete=False).values("id")
        staff_list = [i.get("id") for i in staff_list]
        if department_id not in staff_list:
            raise ValidationError(f"{department_id} 该部门不存在！")

        return attrs